import Timers
import GameType
import Player
import Board
import Move
import Pawn
import Position
import MoveDirection

SIZE_X = 8
SIZE_Y = 8

def getAvalaibleMoves(table, playerForMove):
	jumpMoves = []
	moves = [] 
	for y in range(SIZE_Y):
		for x in range(SIZE_X):
			direct = MoveDirection.ALL_DIRECT
			if (playerForMove == Player.WHITE and table[y][x]==Pawn.WHITE_PAWN) or (playerForMove == Player.BLACK and table[y][x]==Pawn.BLACK_PAWN) :
				direct = Player.getDirection(playerForMove)
			elif (playerForMove == Player.BLACK or table[y][x]!=Pawn.WHITE_DAMKA) and (playerForMove == Player.WHITE or table[y][x]!=Pawn.BLACK_DAMKA) :
				continue
			jumpMoves.extend(checkJumpMove(table,Position.Position(x,y),playerForMove))
			if len(jumpMoves)==0:
				moves.extend(checkSingleMove(table,Position.Position(x,y),direct))	
	if(len(jumpMoves)==0):
		return moves
	return Move.findLongest(jumpMoves)
	
def checkSingleMove(table,position,direct):
	moves = []
	for i in range(-1,2,2):
		directRange = [direct]			# [-1] or [1] for standard Pawn
		if direct == MoveDirection.ALL_DIRECT:
			directRange = [-1,1]		# [-1,1] for damka Pawn move in all directions
		for j in directRange:
			mul = 1
			while True:
				newPosition = Position.Position(position.getX()+i*mul,position.getY()+j*mul)
				if not (checkNewPosition(newPosition) and isEmpty(newPosition,table)):
					break 
				moves.append(Move.createMove(position,[newPosition]))
				if(len(directRange)==1):	#dirty hack for standard Pawn
					break
				mul+=1
	return moves		
	
def checkJumpMove(table,position,playerForMove):
	moves = []
	for i in range(-1,2,2):
		for j in range(-1,2,2):
			mul = 1
			oponentPosition = Position.Position(-1,-1)
			while True:
				newPosition = Position.Position(position.getX()+i*mul,position.getY()+j*mul)
				if not checkNewPosition(newPosition):
					break
				elif isOponent(playerForMove,newPosition,table):
					nextPosition = Position.Position(position.getX()+i*(mul+1),position.getY()+j*(mul+1))
					if checkNewPosition(nextPosition) and isEmpty(nextPosition,table):
						oponentPosition = newPosition.getCopy()
						mul+=1
					else:
						break
				elif isEmpty(newPosition,table):
					if not oponentPosition.getX()==-1 :
						copy = Board.getCopyOfBoard(table)
						copy[newPosition.getY()][newPosition.getX()] = copy[position.getY()][position.getX()]
						copy[position.getY()][position.getX()] = Pawn.EMPTY
						copy[oponentPosition.getY()][oponentPosition.getX()] = Pawn.EMPTY
						moves.append(Move.createMove(position,[newPosition]))
						for tempMove in checkJumpMove(copy,newPosition,playerForMove) :
							move = Move.createMove(position,[newPosition])
							for toPosition in tempMove.getTo():
								move.addTo(toPosition)
							moves.append(move)
					mul+=1 
				else:
					break
				if (not isDamka(position,table)) and mul>2 :	# for standard Pawn jump
					break
	return moves
	
def checkNewPosition(position):
	if position.getX()<0 or position.getY()<0 or position.getX()>(SIZE_X-1) or position.getY()>(SIZE_Y-1):
		return False
	return True
	
def isDamka(position,table):
	tempPawn = table[position.getY()][position.getX()]
	if tempPawn == Pawn.WHITE_DAMKA or tempPawn == Pawn.BLACK_DAMKA :
		return True
	return False
	
def isEmpty(position,table):
	if table[position.getY()][position.getX()]==Pawn.EMPTY:
		return True
	return False
	
def isOponent(playerForMove, oponentPosition,table):
	if playerForMove==Player.BLACK:
		if table[oponentPosition.getY()][oponentPosition.getX()]==Pawn.WHITE_PAWN or table[oponentPosition.getY()][oponentPosition.getX()]==Pawn.WHITE_DAMKA:
			return True
	if playerForMove==Player.WHITE:
		if table[oponentPosition.getY()][oponentPosition.getX()]==Pawn.BLACK_PAWN or table[oponentPosition.getY()][oponentPosition.getX()]==Pawn.BLACK_DAMKA:
			return True
	return False
			
def doMove(move, board):
	tempPawn = board[move.getFrom().getY()][move.getFrom().getX()]
	beforePosition = move.getFrom()
	for moveTo in move.getTo():
		cleanBetweenTwoPoint(beforePosition,moveTo,board) 
		board[moveTo.getY()][moveTo.getX()] = tempPawn
		beforePosition = moveTo
	changeToDamka(board)
	
def cleanBetweenTwoPoint(position1,position2,board):
	tabXChange = []
	tabYChange = []
	if (position2.getX() - position1.getX()) > 0 :
		tabXChange = range(position1.getX(),position2.getX()+1)
	else :
		tabXChange = range(position1.getX(),position2.getX()-1,-1)
	if 	(position2.getY() - position1.getY()) > 0 :
		tabYChange = range(position1.getY(),position2.getY()+1)
	else :
		tabYChange = range(position1.getY(),position2.getY()-1,-1)
	i = 0
	for j in tabXChange:
		board[tabYChange[i]][j]=Pawn.EMPTY
		i+=1	
	
def changeToDamka(board):	#change to Damka all Pawn in first and last row
	firstRow = board[0]
	lastRow = board[SIZE_Y-1]
	for i in range(0,SIZE_X):
		if firstRow[i] == Pawn.BLACK_PAWN :
			firstRow[i] = Pawn.BLACK_DAMKA
		if lastRow[i] == Pawn.WHITE_PAWN :
			lastRow[i] = Pawn.WHITE_DAMKA
	
def changePlayer(actualPlayer):
	if actualPlayer==Player.WHITE:
		return Player.BLACK
	return Player.WHITE
	
def oponentWin(actualPlayer):
	if actualPlayer==Player.WHITE:
		print "Black win"
	if actualPlayer==Player.BLACK:
		print "White win"